//手写实现get请求
function get(url) {
  let xhr = new XMLHttpRequest()
  if (xhr) {
    xhr.open('get', url, true)
    xhr.onreadystatechange = function () {
      if (this.readyState === 4) {
        if (this.status === 200) {
          alert("success")
        } else {
          alert("error")
        }
      }
    }
    xhr.send()
  }
}

//手写实现post请求
function post(url) {
  var req = new XMLHttpRequest();
  if (req) {
    req.open("POST", url, true);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=gbk;");
    req.send("keywords=手机");
    req.onreadystatechange = function () {
      if (req.readyState == 4) {
        if (req.status == 200) {
          alert("success");
        } else {
          alert("error");
        }
      }
    }
  }
}
//promise封装AJAX
function req() {
  return new Promise((resolve, reject) => {
    let xhr = new XMLHttpRequest()
    if (xhr) {
      xhr.open('get', url, true)
      xhr.onreadystatechange = function () {
        if (this.readyState === 4) {
          if (this.status === 200) {
            resolve("success")
          } else {
            reject("error")
          }
        }
      }
      xhr.send()
    }
  });
}
req(url).then(x => { console.log(x) }).catch(x => { console.log(x) })
